/**
 * @Description:
 * @Author: IA ia@kepro.space
 * @Date: 2025-02-20 10:42:35
 */

import { onMounted } from "vue"

type Options = {
  el: string
}

export function useBase64(options: Options): Promise<{ baseUrl: string }> {
  return new Promise((resolve) => {
    onMounted(() => {
      let img: HTMLImageElement = document.querySelector(options.el) as HTMLImageElement
      console.log('28th-1 useBase64 img: ', img)
      img.onload = () => {
        resolve({
          baseUrl: base64(img)
        })
      }

    })

    const base64 = (el: HTMLImageElement) => {
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      canvas.width = el.width
      canvas.height = el.height
      ctx?.drawImage(el, 0, 0, canvas.width, canvas.height)
      return canvas.toDataURL('image/jpeg')
    }
  })


}
